package com.jiangtour.controller;

import com.jiangtour.dataobject.SiteInfo;
import com.jiangtour.enums.ResultEnum;
import com.jiangtour.exception.SiteException;
import com.jiangtour.form.SiteForm;
import com.jiangtour.service.SiteInfoService;
import com.jiangtour.utils.KeyUtil;
import com.jiangtour.utils.MD5;
import com.jiangtour.utils.Numbers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author Administrator
 * @Title: SiteAdminContr
 * @ProjectName site
 * @Description: 总后台控制
 * @date 2018/12/25/02511:37
 */
@RequestMapping("/admin/school")
@Controller
@Slf4j
public class SiteAdminController  {

    @Autowired
    private SiteInfoService siteInfoService;




    /**
     * 站点列表
     * @param page 第几页, 从1页开始
     * @param size 一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

        PageRequest request =PageRequest.of(page - 1, size);
        Page<SiteInfo> siteInfoPage = siteInfoService.findList(request);
        map.put("siteInfoPage", siteInfoPage);
        map.put("currentPage", page);
        map.put("size", size);

        return new ModelAndView("admin/list", map);
    }

    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "siteId", required = false) String siteId,
                               Map<String, Object> map) {

        if(null != siteId){
            SiteInfo siteInfo = siteInfoService.findOne(siteId);
            map.put("siteInfo", siteInfo);
        }

        return new ModelAndView("admin/detail", map);
    }

    /**
     * 修改密码
     * @param siteId
     * @param map
     * @return
     */
    @GetMapping("/update")
    public ModelAndView update(@RequestParam(value = "siteId", required = false) String siteId,
                               Map<String, Object> map) {

        if(null != siteId){
            SiteInfo siteInfo = siteInfoService.findOne(siteId);
            map.put("siteInfo", siteInfo);
        }

        return new ModelAndView("admin/update", map);
    }



    /**
     * 启用
     * @param siteId
     * @param map
     * @return
     */
    @RequestMapping("/on_use")
    public ModelAndView onUse(@RequestParam("siteId") String siteId,
                               Map<String, Object> map) {
        try {
            siteInfoService.onUse(siteId);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/admin/school/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/admin/school/list");
        return new ModelAndView("common/success", map);

    }

    @PostMapping("/update_password")
      public ModelAndView updatePassword(@RequestParam("id") String id,
                                         String password,
                                         Map<String, Object> map){

        try {
            siteInfoService.updatePassword(id,password);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/admin/school/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/admin/school/list");
        return new ModelAndView("common/success", map);
    }


    /**
     * 禁用
     * @param siteId
     * @param map
     * @return
     */
    @RequestMapping("/off_use")
    public ModelAndView offUse(@RequestParam("siteId") String siteId,
                              Map<String, Object> map) {
        try {
            siteInfoService.offUse(siteId);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/admin/school/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/admin/school/list");
        return new ModelAndView("common/success", map);

    }

    /**
     * 保存 更新
     * @param form
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid SiteForm form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/admin/school/detail");
            return new ModelAndView("common/error", map);
        }
        SiteInfo siteInfo = new SiteInfo();
        try {
            if( !StringUtils.isEmpty(form.getId())){
                siteInfo = siteInfoService.findOne(form.getId());

            }else {
                form.setId(KeyUtil.getUniqueKey());
                SiteInfo info = siteInfoService.queryByAccount(form.getAccount());
                if(info !=null ){
                    throw  new SiteException(ResultEnum.SITE_ALREADY_EXISTED);
                }

            }

            BeanUtils.copyProperties(form,siteInfo);

            siteInfoService.create(siteInfo);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/admin/school/detail");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/admin/school/list");
        return new ModelAndView("common/success", map);
    }




    /**
     * 高德地图
     */
    @RequestMapping("map")
    public String map() {
        return "redirect:/commonModule/map.jsp";
    }






}
